$(function () {
    var identification1 = uuid();
    $("#identification").val(identification1);
    $("#yzm").attr("src", "http://localhost:8081/study/photo/CreateYZM?identification=" + $("#identification").val());

    //更新验证码
    $("#updateYZM").click(function () {
        var identification = uuid();
        $("#yzm").attr("src", "http://localhost:8081/study/photo/CreateYZM?identification=" + identification);
        $("#identification").val(identification);
    })

    $("#zz").click(function () {
        var confirmMID = uuid();
        console.log(confirmMID);
        if (window.localStorage) {
            // localStorge可用
            // 获取本地存储对象
            var localStorage = window.localStorage;
            // 存储
            localStorage.setItem("key", confirmMID);
        } else {
            console.log("confirmMID 不能存到本地") // localStorge不可用
        }
        var username = $("#username").val();
        var password = $("#password").val();
        var yzmcode = $("#yzmCode").val();
        var identification = $("#identification").val();
        console.log(yzmcode);
        //验证验证码
        $.post("http://localhost:8081/study/photo/confirmYZM", {
            identification: identification,
            code: yzmcode
        }, function (data) {
            if (data.success) {
                $.post("http://localhost:8081/study/login", {
                    username: username,
                    password: password,
                    yzmCode: yzmcode,
                    identification: identification,
                    confirmMID: confirmMID
                }, function (data) {
                    console.log(data);
                    if (data.success) {
                        window.location.href = "html/index.html";
                    } else {
                        alert(data.message);
                        return;
                    }
                })
            } else {
                alert(data.message)
                return;
            }
        })

    })

    //禁用form表单自定义action提交功能
    $(document).ready(function () {
        $("form").submit(function (e) {
            e.preventDefault();
            console.log("Submit prevented");
        });
    });

})


function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}